﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NuGet.Common;
using NPOI.Util;
using XuanKeZz.Application.HTTP;
using WMSUIMVC.DTO.Vendor;

namespace WMSUIMVC.Controllers
{
    public class VendorController : Controller
    {
        protected readonly IConfiguration _configuration;

        public VendorController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 显示供应商
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowVendor()
        {
            return View();
        }

        /// <summary>
        /// 供应商显示
        /// </summary>
        /// <param name="vendor"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetVendorShow(SelVendorDTO vendor)
        {
            HttpClientWrapper client = new HttpClientWrapper(vendor.Token);
            string url = _configuration["Url"] + "ywd/api/Vendor/GetShowVendor?VendorCode=" + vendor.VendorCode + "&VendorName="+ vendor.VendorName + "&VendorType="+ vendor.VendorType + "&Vphone="+ vendor.Vphone + "&pageindex=" + vendor.PageIndex + "&pagesize=" + vendor.PageSize;
            //string list = JsonConvert.SerializeObject(vendor);
            string GetVendor = await client.GetAsync(url);
            return Json(GetVendor);
        }
        /// <summary>
        /// 添加供应商信息
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> AddVendors(AddVendorDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"] + "ywx/api/Vendor/AddVendor";
            string datas = JsonConvert.SerializeObject(obj);
            var res = await client.PostAsync(url, datas);
            return Json(res);
        }
        /// <summary>
        /// 供应商下拉框
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        public async Task<ActionResult> GetDropVendors(string Token)
        {
            HttpClientWrapper client = new HttpClientWrapper(Token);
            string url = _configuration["Url"] + "ywd/api/Vendor/GetDropVendor";
            var res = await client.GetAsync(url);
            return Json(res);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="vendor"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DelAllVendor(DelVendorDTO delvendor)
        {
            HttpClientWrapper client = new HttpClientWrapper(delvendor.Token);
            string url = _configuration["Url"] + "ywx/api/Vendor/DelVendor";
            string list = JsonConvert.SerializeObject(delvendor);
            var res = await client.PostAsync(url, list);
            return Json(res);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="fvendor"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetVendorFt(FanVendorDTO fvendor)
        {
            HttpClientWrapper client = new HttpClientWrapper(fvendor.Token);
            string url = _configuration["Url"] + "ywd/api/Vendor/GetVendorById?VendorId=" + fvendor.VendorId;
            string GetUnit = await client.GetAsync(url);
            return Json(GetUnit);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="uvendor"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdVendor(UpdVendorDTO uvendor)
        {
            HttpClientWrapper client = new HttpClientWrapper(uvendor.Token);
            string url = _configuration["Url"] + "ywx/api/Vendor/UpdVendor";
            string list = JsonConvert.SerializeObject(uvendor);
            string UpdVendor = await client.PutAsync(url, list);
            return Json(UpdVendor);
        }
    }
}
